package com.fypg.module.medical.controller.admin.registerorderrefund;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.fypg.framework.common.pojo.PageParam;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.pojo.CommonResult;
import com.fypg.framework.common.util.object.BeanUtils;
import static com.fypg.framework.common.pojo.CommonResult.success;

import com.fypg.framework.excel.core.util.ExcelUtils;

import com.fypg.framework.apilog.core.annotation.ApiAccessLog;
import static com.fypg.framework.apilog.core.enums.OperateTypeEnum.*;

import com.fypg.module.medical.controller.admin.registerorderrefund.vo.*;
import com.fypg.module.medical.dal.dataobject.outpatientorderdetail.registerorderrefund.RegisterOrderRefundDO;
import com.fypg.module.medical.service.registerorderrefund.RegisterOrderRefundService;

@Tag(name = "管理后台 - 挂号订单退款信息")
@RestController
@RequestMapping("/medical/register-order-refund")
@Validated
public class RegisterOrderRefundController {

    @Resource
    private RegisterOrderRefundService registerOrderRefundService;

    @PostMapping("/create")
    @Operation(summary = "创建挂号订单退款信息")
    @PreAuthorize("@ss.hasPermission('medical:register-order-refund:create')")
    public CommonResult<Long> createRegisterOrderRefund(@Valid @RequestBody RegisterOrderRefundSaveReqVO createReqVO) {
        return success(registerOrderRefundService.createRegisterOrderRefund(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新挂号订单退款信息")
    @PreAuthorize("@ss.hasPermission('medical:register-order-refund:update')")
    public CommonResult<Boolean> updateRegisterOrderRefund(@Valid @RequestBody RegisterOrderRefundSaveReqVO updateReqVO) {
        registerOrderRefundService.updateRegisterOrderRefund(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除挂号订单退款信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('medical:register-order-refund:delete')")
    public CommonResult<Boolean> deleteRegisterOrderRefund(@RequestParam("id") Long id) {
        registerOrderRefundService.deleteRegisterOrderRefund(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除挂号订单退款信息")
                @PreAuthorize("@ss.hasPermission('medical:register-order-refund:delete')")
    public CommonResult<Boolean> deleteRegisterOrderRefundList(@RequestParam("ids") List<Long> ids) {
        registerOrderRefundService.deleteRegisterOrderRefundListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得挂号订单退款信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('medical:register-order-refund:query')")
    public CommonResult<RegisterOrderRefundRespVO> getRegisterOrderRefund(@RequestParam("id") Long id) {
        RegisterOrderRefundDO registerOrderRefund = registerOrderRefundService.getRegisterOrderRefund(id);
        return success(BeanUtils.toBean(registerOrderRefund, RegisterOrderRefundRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得挂号订单退款信息分页")
    @PreAuthorize("@ss.hasPermission('medical:register-order-refund:query')")
    public CommonResult<PageResult<RegisterOrderRefundRespVO>> getRegisterOrderRefundPage(@Valid RegisterOrderRefundPageReqVO pageReqVO) {
        PageResult<RegisterOrderRefundDO> pageResult = registerOrderRefundService.getRegisterOrderRefundPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RegisterOrderRefundRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出挂号订单退款信息 Excel")
    @PreAuthorize("@ss.hasPermission('medical:register-order-refund:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRegisterOrderRefundExcel(@Valid RegisterOrderRefundPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RegisterOrderRefundDO> list = registerOrderRefundService.getRegisterOrderRefundPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "挂号订单退款信息.xls", "数据", RegisterOrderRefundRespVO.class,
                        BeanUtils.toBean(list, RegisterOrderRefundRespVO.class));
    }

}
